package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.NewsInfoAddDTO;
import org.jsola.hr.dto.NewsInfoUpdateDTO;
import org.jsola.hr.query.NewsInfoQuery;
import org.jsola.hr.service.INewsInfoService;
import org.jsola.hr.vo.NewsInfoListVO;
import org.jsola.hr.vo.NewsInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 官网信息的类型
 *
 * @author zhr
 */
@RestController("hrNewsInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/newsInfos")
public class NewsInfoController implements INewsInfoControllerAPI {

    @Autowired
    private INewsInfoService newsInfoService;

    @Override
    @PostMapping
    public Result<NewsInfoVO> save(@Valid @RequestBody NewsInfoAddDTO newsInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增官网信息的类型权限校验

        return Result.success(
                newsInfoService.save(newsInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody NewsInfoUpdateDTO newsInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改官网信息的类型权限校验

        int count = newsInfoService.update(newsInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{newsInfoId}")
    public Result<Integer> delete(@PathVariable String newsInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除官网信息的类型权限校验

        int count = newsInfoService.deleteByIds(tokenUser, newsInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] newsInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除官网信息的类型权限校验

        int count = newsInfoService.deleteByIds(tokenUser, newsInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{newsInfoId}")
    public Result<NewsInfoVO> findById(@PathVariable String newsInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网信息的类型权限校验

        return Result.success(
            newsInfoService.selectById(newsInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<NewsInfoListVO>> listAll(@Valid NewsInfoQuery newsInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网信息的类型权限校验

        return Result.success(
            newsInfoService.select(newsInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<NewsInfoListVO>> page(@Valid NewsInfoQuery newsInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            newsInfoService.selectPage(newsInfoQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectNewsInfoByNewsTypeId")
    public Result<NewsInfoListVO> selectNewsInfoByNewsTypeId(String newsTypeId) {
        // todo 查看官网信息的类型权限校验

        return Result.success(
                newsInfoService.selectNewsInfoByNewsTypeId(newsTypeId)
        );
    }

}

